package com.example.algorithm.tree;

import java.util.ArrayList;
import java.util.Collections;
import java.util.List;
import java.util.Stack;

/**
 * @author : zhy
 * @since : 2022/7/29
 * 后序广度遍历（与前序广度遍历相反）
 */
public class AfterForeach {

    public List<Integer> postorderTraversal(TreeNode root) {
        List<Integer> list=new ArrayList<>();
        if(root==null)
        {
            return list;
        }
        Stack<TreeNode> stack=new Stack<>();
        stack.push(root);
        while(!stack.isEmpty())
        {
            TreeNode pop = stack.pop();
            list.add(pop.val);
            if(pop.left!=null)
            {
                stack.push(pop.left);
            }
            if(pop.right!=null)
            {
                stack.push(pop.right);
            }
        }
        Collections.reverse(list);
        return list;
    }

}
